﻿package com.sogoodsoft.web;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.service.common.AbstractService;
import com.sogoodsoft.util.DwzMsg;
import com.sogoodsoft.web.common.Report;

@SuppressWarnings("serial")
public abstract class BaseAction<T, PK extends Serializable> extends CrudActionSupport < T > {

    protected T entity;

    protected PK id;

    protected String orderby = "id";

    protected String order = Page.DESC;

    protected Page < T > page;

    protected  List < PropertyFilter > filters ;
    
    protected Class < T > entityClass;
    
    protected Report report;

    @SuppressWarnings("unchecked")
    public BaseAction() {
        super();
        entityClass = (Class < T >) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        page = new Page < T >(WebConstants.PAGE_SIZE);
        report = new Report();
    }

    @SuppressWarnings("unchecked")
    @Override
    public String delete() throws Exception {
        getService().delete(id);
        Struts2Utils.renderJson(DwzMsg.success());
        return null;
    }

    @Override
    public String input() throws Exception {
        return INPUT;
    }

    @SuppressWarnings("unchecked")
    @Override
    public String list() throws Exception {
        filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
        fixFilter();
        // 设置默认排序方式
        if (!page.isOrderBySetted()) {
            page.setOrderBy(orderby);
            page.setOrder(order);
        }

        logger.debug("查询列表");
        page = getService().findPage(page, filters);
        return SUCCESS;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = (T) getService().get(id);
        } else {
            entity = entityClass.newInstance();
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public String save() throws Exception {
        onSave();
        getService().save(entity);
        Struts2Utils.renderJson(DwzMsg.successAndX(Struts2Utils.getParameter(DwzMsg.param_navTabId)));
        return null;
    }

    @Override
    public T getModel() {
        return entity;
    }

    public T getEntity() {
        return entity;
    }

    public void setEntity(T entity) {
        this.entity = entity;
    }

    public PK getId() {
        return id;
    }

    public void setId(PK id) {
        this.id = id;
    }

    public Page < T > getPage() {
        return page;
    }

    public void setPage(Page < T > page) {
        this.page = page;
    }

    public String getOrderby() {
        return orderby;
    }

    public void setOrderby(String orderby) {
        this.orderby = orderby;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }

    @SuppressWarnings("rawtypes")
    protected abstract AbstractService getService();

    /**
     * <p>
     * 可重载，调用save方法前，自动调用
     * </p>
     * <p>
     * 用于载入完成entity的关联类型
     * </p>
     */
    protected void onSave() {
    }

    /**
     * 添加额外查询条件
     */
    protected void fixFilter(){}
    
    
}
